package presentation.entities;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.tree.TreePath;
import presentation.TableFilteringScreen;
import presentation.TreePopUpMenu;

public class TreeMouseListner implements MouseListener {

    TableFilteringScreen dbFileterScreen;

    public TreeMouseListner(TableFilteringScreen dbFileterScreen) {
        this.dbFileterScreen = dbFileterScreen;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
        TreePath path = dbFileterScreen.getLeadSelectedPath();
        if (path != null) {
            dbFileterScreen.setFilterText(path);
            if (e.isPopupTrigger()) {
                doPop(e, path);
            }
        }

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        TreePath path = dbFileterScreen.getLeadSelectedPath();
        if (path != null) {
            dbFileterScreen.setFilterText(path);
            if (e.isPopupTrigger()) {
                doPop(e, path);
            }
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    private void doPop(MouseEvent e, TreePath path) {
        TreePopUpMenu menu = new TreePopUpMenu(dbFileterScreen, path);
        menu.show(e.getComponent(), e.getX(), e.getY());
    }
}
